package com.tiyende.kash.loop.ui.presenter

import com.tiyende.kash.loop.common.CommonPresenter
import com.tiyende.kash.loop.configs.TiyendeApiPath
import com.tiyende.kash.loop.configs.TiyendeApiUrl
import com.tiyende.kash.loop.configs.TiyendeApiUrl.addCommonParams
import com.tiyende.kash.loop.request.IHttpCallback
import com.tiyende.kash.loop.ui.contract.IViewContract
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/27 13:45
 * @Annotation：支付状态查询 Presenter 实现
 */
class TiyendeOrderPayStatusPresenter(private val iView: IViewContract.ITiyendeOrderPayStatusView) : CommonPresenter(iView) {

    //三方订单id
    private var thirdOrderId = ""
    fun saveOrderPayPinThirdOrderId(orderId: String){
        thirdOrderId = orderId
    }
    fun queryTiyendeThirdOrderId(): String{
        return thirdOrderId
    }

    /**
     * 获取支付状态
     */
    fun queryTiyendeOrderPayStatusAction(isStartCountdown: Boolean = true){
        iView.showTiyendeLoading()
        val params = JSONObject().addCommonParams().apply {
            put("path",TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_LOAN_REPAYMENT_STATUS_PATH)
            put("thirdOrderId",thirdOrderId)//三方订单id
        }
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.hiddenTiyendeLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    obj.put("isStartCountdown",isStartCountdown)
                    if (obj.has("transactionId")){
                        thirdOrderId = obj.optString("transactionId")
                    }
                    iView.showTiyendeOrderPayStatusCommonView(obj)
                    // PROCESSING /SUCCEEDED /FAILED
                    val status = obj.optString("status")
                    when(status){
                        "FAILED" -> {
                            iView.showTiyendeOrderPayStatusFailedView(obj)
                        }
                        "SUCCEEDED" -> {
                            iView.showTiyendeOrderPayStatusSuccessView(obj)
                        }
                        else -> {
                            iView.showTiyendeOrderPayStatusProcessView(obj)
                        }
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }

    /**
     * 订单还款
     */
    fun queryTiyendeOrderPayAction(){
        iView.showTiyendeLoading()
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("orderId",iView.getTiyendeOrderPayStatusOrderId())
            put("path",TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_LOAN_REPAYMENT_LOOP_URL_PATH)
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.hiddenTiyendeLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null && obj.has("thirdOrderId")){
                    thirdOrderId = obj.optString("thirdOrderId")
                }
                queryTiyendeOrderPayStatusAction()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

}